package gsm.cable.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class UserWidget extends Composite {
	private static UserWidgetUiBinder uiBinder = GWT.create(UserWidgetUiBinder.class);

	@UiField
	TextBox userName;

	@UiField
	PasswordTextBox password;

	@UiField
	Button loginButton;

	// @UiField
	// HTML organizationSlogan;

	// @UiField
	// ScrollPanel bodyPanel;

	// @UiField
	// HTML copyrightMessage;

	// @UiField
	// Anchor signInLink;

	interface UserWidgetUiBinder extends UiBinder<Widget, UserWidget> {
	}

	public UserWidget() {
		initWidget(uiBinder.createAndBindUi(this));
		loginButton.addClickHandler(new LoginHandlerImpl(userName, password));
		Window.addResizeHandler(new ResizeHandler() {

			@Override
			public void onResize(ResizeEvent event) {
				// Window.alert("resize");
			}
		});
	}

	private class LoginHandlerImpl implements ClickHandler {
		private TextBox userName;
		private PasswordTextBox password;

		public LoginHandlerImpl(TextBox userName, PasswordTextBox password) {
			this.userName = userName;
			this.password = password;
		}

		public void onClick(ClickEvent event) {
			String userName = this.userName.getText();
			String password = this.password.getText();
			if (isUserNameValid(userName) && isPasswordValid(password)) {
				login(userName, password);
				
			} else {
				Window.alert("Invalid login");
			}

		}

		private void login(String userName, String password) {
			try {
				String USER_NAME = "username";
				String PASSWORD = "password";
				String loginUrl = "/";
				JSONObject parameter = new JSONObject();
				JSONObject loginInfo = new JSONObject();
				loginInfo.put(USER_NAME, new JSONString(userName));
				loginInfo.put(PASSWORD, new JSONString(password));
				parameter.put("login", loginInfo);
				RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, loginUrl);
				String requestData = "parameter=" + parameter.toString();
				Window.alert(requestData);
				builder.sendRequest(requestData, new LoginCallback());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		boolean isPasswordValid(String password) {
			if (password != null && password.length() >= 6) {
				return true;
			}
			return false;
		}

		boolean isUserNameValid(String userName) {
			if (userName != null && userName.trim().length() > 0) {
				return true;
			}
			return false;
		}
	}

	private class LoginCallback implements RequestCallback {

		@Override
		public void onResponseReceived(Request request, Response response) {
			String text = response.getText();
			// JSONObject object = (JSONObject) JSONParser.parse(text);
			Window.alert("response : " + text);
			RootPanel simplePanel = RootPanel.get("viewcontainer");
			AdminWidget adminWidget = new AdminWidget();
			simplePanel.remove(0);
			simplePanel.add(adminWidget);
		}

		@Override
		public void onError(Request request, Throwable exception) {
			exception.printStackTrace();
			Window.alert("Exception from server : " + exception.getMessage());
		}
	}
}
